import UserModel
import LinRel
import os

'''
-----------------------------------------------------------------------------------------
Class to test the performance of LinRel algorithm 
For using, determine:
File = 'features_lewis'
numberofImages = 1000
setsize = 10
expnumber = 100
Writes statistics to /Statistics/filename number_of_images_LinRel.average(chosen,minimum)
------------------------------------------------------------------------------------------
'''

class Experiment(object):

    MAXDISTANCE = 0.01
    MAXITERATIONS = 100

    def __init__(self, filename, number_of_images, setsize=10):
        self.setsize = setsize
        self.number_of_images = number_of_images
        self.user = UserModel.UserModel(self.number_of_images, self.setsize, filename)
        self.target = self.user.getTarget()
        print 'Target = ', self.target
        self.path = os.path.dirname(__file__) + '-files/'
        self.input_average = self.path + filename + str(self.number_of_images) + '_' + 'LR.average'
        self.input_chosen = self.path + filename + str(self.number_of_images) + '_' + 'LR.chosen'
        self.input_minimum = self.path + filename + str(self.number_of_images) + '_' + 'LR.minimum'

        self.lr = LinRel.LinRel(filename, number_of_images)
        
        
    def Run(self):
        average_distances = []
        chosen_distances = []
        minimum_distances = []
        
        chosen_image = None
      #  chosen_dist = 1
        iteration = 1
        # First iteration
        images = self.lr.ChooseFirstImageSet(setsize)
 #       print 'Images = ', images
        chosen_image, relevance_scores, average_dist, chosen_dist, minimum_dist = self.user.Select(images)
#        print 'Chosen image = ', chosen_image
 
        average_distances.append(average_dist)
        chosen_distances.append(chosen_dist)
        minimum_distances.append(minimum_dist)
                
        while(chosen_dist > Experiment.MAXDISTANCE) and iteration < Experiment.MAXITERATIONS:
       #     print 'Iteration ', iteration
            iteration += 1
            
            #In oder to check how random algorithms performs comparing to LinRel
            #images = self.lr.ChooseFirstImageSet(setsize)
            images = self.lr.LinRel(images, relevance_scores, setsize)
  #          print 'Images = ', images
            chosen_image, relevance_scores, average_dist, chosen_dist, minimum_dist = self.user.Select(images)
            # Collect statistics
            average_distances.append(average_dist)
            chosen_distances.append(chosen_dist)
            minimum_distances.append(minimum_dist)
  #          print 'Chosen image = ', chosen_image
        Terminated = False
        if iteration < Experiment.MAXITERATIONS:
            print 'We have found the target!'
            print 'Number of iterations = ', iteration
            Terminated = True 
            while len(chosen_distances) < Experiment.MAXITERATIONS: 
                average_distances.append(0)
                chosen_distances.append(0)
                minimum_distances.append(0)
        return Terminated, average_distances, chosen_distances, minimum_distances, iteration

File = 'features-1000'

numberofImages = 1000
setsize = 10

expnumber = 100

iterations = 0
for i in range(expnumber):
    exp = Experiment(File, numberofImages, setsize)
    Terminated, average_distance, chosen_distance, minimum_distance, iteration = exp.Run()
    iterations += iteration
    if i==0:
        # Clear files
        f=open(exp.input_average,  'w')
        f.writelines('')
        f.close()
        f=open(exp.input_chosen,  'w')
        f.writelines('')
        f.close()
        f=open(exp.input_minimum,  'w')
        f.writelines('')
        f.close()

    
    str_average_distance = map(str, average_distance)
    average_distance_write = ' '.join(str_average_distance)+'\n'
    f=open(exp.input_average,  'a')
    f.writelines(average_distance_write)
    
    str_chosen_distance = map(str, chosen_distance)
    chosen_distance_write = ' '.join(str_chosen_distance)+'\n'
    f=open(exp.input_chosen,  'a')
    f.writelines(chosen_distance_write)
    
    str_minimum_distance = map(str, minimum_distance)
    minimum_distance_write = ' '.join(str_minimum_distance)+'\n'
    f=open(exp.input_minimum,  'a')
    f.writelines(minimum_distance_write)
    
print 'Average number of iteration = ', 1.0*iterations/expnumber